//
//  LaunchAdManager.m
//  CToolBox
//
//  Created by 张吉旭 on 17/3/18.
//  Copyright © 2017年 yj. All rights reserved.
//

#import "LaunchAdManager.h"
#import <XHLaunchAd.h>
#import "DTOLaunchAdModel.h"
#import "LaunchWebViewController.h"

@interface LaunchAdManager()<XHLaunchAdDelegate>

@end

@implementation LaunchAdManager

+(LaunchAdManager*)shareInstance
{
    static LaunchAdManager * instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LaunchAdManager alloc] init];
    });
    
    return instance;
}


-(void)initLaunch
{
    [self setupPage];
}



-(void)setupPage
{
    
    //广告数据转模型
    NSDictionary * dic = DEF_PERSISTENT_GET_OBJECT(DEF_PERSISTENT_LAUNCHAD);
    DTOLaunchAdModel *model = [[DTOLaunchAdModel alloc] initWithDictionary:dic error:nil];
    
    if (model && model.TimeSec>0 && ![model.ImageUrl isEqualToString:@""]) {
        _isShowTime = YES;
        [XHLaunchAd setWaitDataDuration:1];
        
        //配置广告数据
        XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
        //广告停留时间
        imageAdconfiguration.duration = model.TimeSec.intValue;
        //广告frame
        imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width/1242*2208);;
        //缓存机制
        imageAdconfiguration.imageOption = XHLaunchAdImageDefault;
        //图片填充模式
        imageAdconfiguration.contentMode = UIViewContentModeScaleToFill;
        //广告显示完成动画
        imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
        //跳过按钮类型
//        imageAdconfiguration.skipButtonType = SkipTypeTimeText;
        //start********************自定义跳过按钮**************************
        imageAdconfiguration.customSkipView = [self customSkipView];
        //后台返回时,是否显示广告
        imageAdconfiguration.showEnterForeground = NO;
        
        //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
        imageAdconfiguration.imageNameOrURLString = model.ImageUrl;
        //广告点击打开链接
        if ([model.H5Url isEqualToString:@""]) {
            imageAdconfiguration.openURLString = nil;
        }else{
            imageAdconfiguration.openURLString = model.H5Url;
        }
        
        //显示图片开屏广告
        [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
        
        
    }
    
}


/**
 *  广告点击事件 回调
 */
- (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenURLString:(NSString *)openURLString;
{
    NSLog(@"广告点击");
    
    LaunchWebViewController * webViewControll = [[LaunchWebViewController alloc] initWithWebUrl:openURLString];
    
    [webViewControll setModalPresentationStyle:UIModalPresentationCurrentContext];
    [webViewControll setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    
    
    [[AppDelegate appDelegate].window.rootViewController presentViewController:webViewControll animated:YES completion:nil];
}


#pragma mark - customSkipView
//自定义跳过按钮
-(UIView *)customSkipView
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.frame = CGRectMake(WIDTH-65, 30, 60, 20);
    [button addTarget:self action:@selector(skipAction) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
//跳过按钮点击事件
-(void)skipAction
{
    _isShowTime = NO;
    [XHLaunchAd skipAction];
}
#pragma mark - XHLaunchAd delegate - 倒计时回调
/**
 *  倒计时回调
 *
 *  @param launchAd XHLaunchAd
 *  @param duration 倒计时时间
 */
-(void)xhLaunchAd:(XHLaunchAd *)launchAd customSkipView:(UIView *)customSkipView duration:(NSInteger)duration
{
    //设置自定义跳过按钮时间
    UIButton *button = (UIButton *)customSkipView;//此处转换为你之前的类型
    
    //设置时间
    [button setTitle:[NSString stringWithFormat:@"%lds 跳过",(long)duration] forState:UIControlStateNormal];
}

-(void)xhLaunchShowFinish:(XHLaunchAd *)launchAd
{
    _isShowTime = NO;
//    [AppDelegate showByLoginState];
    
}

@end
